/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.personnel.secretasset.controller;

import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.entity.secretasset.SecretAsset;
import com.fuyao.cloud.asset.entity.secretasset.SecretAssetExportVO;
import com.fuyao.cloud.asset.entity.secretasset.SecretAssetImportVO;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.secretasset.service.SecretAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 涉密设备信息
 *
 * @author lyhills
 * @date 2023-04-12 13:59:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/secretasset")
@Api(value = "secretasset", tags = "涉密设备信息管理")
public class SecretAssetController {

    private final SecretAssetService secretAssetService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param secretAsset 涉密设备信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_view')")
    public R getSecretAssetPage(Page page, SecretAsset secretAsset) {
        return R.ok(secretAssetService.page(page, SpliceQueryWrapper.spliceSearchCondition(secretAsset)));
    }


    /**
     * 通过id查询涉密设备信息
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(secretAssetService.getById(id));
    }

    /**
     * 新增涉密设备信息
     *
     * @param secretAsset 涉密设备信息
     * @return R
     */
    @ApiOperation(value = "新增涉密设备信息", notes = "新增涉密设备信息")
    @SysLog("新增涉密设备信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_add')")
    public R save(@RequestBody SecretAsset secretAsset) {
        return R.ok(secretAssetService.save(secretAsset));
    }

    /**
     * 修改涉密设备信息
     *
     * @param secretAsset 涉密设备信息
     * @return R
     */
    @ApiOperation(value = "修改涉密设备信息", notes = "修改涉密设备信息")
    @SysLog("修改涉密设备信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_edit')")
    public R updateById(@RequestBody SecretAsset secretAsset) {
        return R.ok(secretAssetService.updateById(secretAsset));
    }

    /**
     * 通过id删除涉密设备信息
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除涉密设备信息", notes = "通过id删除涉密设备信息")
    @SysLog("通过id删除涉密设备信息")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(secretAssetService.removeById(id));
    }


    /**
     * 导出excel 表格
     *
     * @param secretAsset 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_export')")
    public List<SecretAssetExportVO> export(SecretAsset secretAsset) {
        return secretAssetService.listExport(SpliceQueryWrapper.spliceSearchCondition(secretAsset));
    }

    /**
     * 导入预约信息
     *
     * @param excelVOList   用户列表
     * @param bindingResult 错误信息列表
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_export')")
    public R importUser(@RequestExcel List<SecretAssetImportVO> excelVOList, BindingResult bindingResult) {
        return secretAssetService.importSecretAsset(excelVOList, bindingResult);
    }

    /**
     * 获取本地（resources）文件
     *
     * @param fileName 文件名称
     * @param response 本地文件
     */
    @SneakyThrows
    @GetMapping("/local/{fileName}")
    public void localFile(@PathVariable String fileName, HttpServletResponse response) {
        ClassPathResource resource = new ClassPathResource("file/" + fileName);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IoUtil.copy(resource.getInputStream(), response.getOutputStream());
    }

    @ApiOperation(value = "通过ids批量删除", notes = "通过ids批量删除")
    @SysLog("通过id删除")
    @DeleteMapping("/batchDeleteIds")
    @PreAuthorize("@pms.hasPermission('secretasset_secretasset_del')")
    public R batchDeleteIds(@RequestBody List<Long> ids) {
        return R.ok(this.secretAssetService.removeByIds(ids));
    }
}
